//
//  BCCalendarViewController.m
//  Memo
//
//  Created by bonc on 13-9-2.
//  Copyright (c) 2013年 fuzhengbang. All rights reserved.
//

#import "BCCalendarViewController.h"
#import "LunarCalendar.h"
#import "NSDate+Calendar.h"
#import "BCNoteDisplayViewController.h"
#import "BCDateBase.h"
#import "BCAddNoteViewController.h"
#import "BCSettingsViewController.h"


@interface BCCalendarViewController ()
@property (strong,nonatomic) UILabel* dayLabel;
@property (strong,nonatomic) UILabel* lunarLabel;
@end

@implementation BCCalendarViewController
@synthesize calendarView;
@synthesize dayLabel;
@synthesize lunarLabel;

-(void)setDisplayDate:(NSDate *)selectedDate
{
    _displayDate=selectedDate;
    LunarCalendar*  lunarCalendar=[selectedDate chineseCalendarDate];
    [lunarCalendar loadWithDate:selectedDate];
    dayLabel.text=[NSString stringWithFormat:@"%d年%d月",[lunarCalendar GregorianYear],[lunarCalendar GregorianMonth]];
    lunarLabel.text=[NSString stringWithFormat:@"%@%@年%@%@月",[lunarCalendar YearHeavenlyStem],[lunarCalendar YearEarthlyBranch],[lunarCalendar MonthHeavenlyStem],[lunarCalendar MonthEarthlyBranch]];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    if (IOS_VERSION_Above_7_0) {
//        self.edgesForExtendedLayout=UIRectEdgeNone;
//    }
	// Do any additional setup after loading the view.
    //导航栏左按钮
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(goSettings:)];
    //导航栏右按钮组
    UIBarButtonItem* todayButton=[[UIBarButtonItem alloc]initWithTitle:@"今天" style:UIBarButtonItemStylePlain target:self action:@selector(backToday:)];
    UIBarButtonItem* addButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewMemo:)];
    self.navigationItem.rightBarButtonItems=@[addButton,todayButton];
    //导航栏的标题视图
    UIView* titleView=[[UIView alloc]initWithFrame:CGRectMake(0, 2, 80, 40)];
    dayLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
    dayLabel.textAlignment=UITextAlignmentCenter;
    dayLabel.font=[UIFont boldSystemFontOfSize:13];
    dayLabel.backgroundColor=[UIColor clearColor];
    dayLabel.userInteractionEnabled=YES;
    [titleView addSubview:dayLabel];
    lunarLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 80, 20)];
    lunarLabel.textAlignment=UITextAlignmentCenter;
    lunarLabel.font=[UIFont boldSystemFontOfSize:13];
    lunarLabel.backgroundColor=[UIColor clearColor];
    lunarLabel.text=@"辛丑年甲子月";
    lunarLabel.userInteractionEnabled=YES;
    
    [titleView addSubview:lunarLabel];
    if (IOS_VERSION_Above_7_0) {
        dayLabel.textColor=[UIColor blackColor];
        lunarLabel.textColor=[UIColor blackColor];
    }else{
    dayLabel.textColor=[UIColor whiteColor];
    lunarLabel.textColor=[UIColor whiteColor];
    }

    self.navigationItem.titleView=titleView;
    UITapGestureRecognizer *tapGR=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickTime:)];
    [titleView addGestureRecognizer:tapGR];
    //添加日历视图
    calendarView=[[JBUnitView alloc]initWithFrame:self.view.frame UnitType:UnitTypeMonth SelectedDate:[NSDate date] AlignmentRule:JBAlignmentRuleTop Delegate:self DataSource:self];
    [self.view addSubview:calendarView];
    self.displayDate=[NSDate date];
}

-(void)viewWillAppear:(BOOL)animated
{
    [self.calendarView reloadEvents];
}

//进入设置页面
- (void)goSettings:(id) sender
{
    NSLog(@"go settings");
    BCSettingsViewController* settingsVC=[[BCSettingsViewController alloc]init];
    [self.navigationController pushViewController:settingsVC animated:YES];
}

//添加新的备忘
-(void)addNewMemo:(id) sender
{
    BCAddNoteViewController* addVC=[[BCAddNoteViewController alloc]init];
    [self.navigationController pushViewController:addVC animated:YES];
    NSLog(@"add a new memo");
}

//拾取时间
-(void)pickTime:(id) sender
{
    NSLog(@"go and pick a time");
    NSDate* maxTime=[NSDate dateWithYear:2050 Month:12 Day:31 Hour:0 Minute:0];
    NSDate* minTime=[NSDate dateWithYear:1900 Month:1 Day:1 Hour:0 Minute:0];
    BCDatePickerController* pickerVC=[[BCDatePickerController alloc]initTimeRangeFromDate:minTime ToDate:maxTime WithType:PickerTypeDate];
    pickerVC.date=self.displayDate;
    pickerVC.delegate=self;
    [self.navigationController pushViewController:pickerVC animated:YES];
}

//回到今日日历
-(void)backToday:(id) sender
{
    NSDate* today=[NSDate date];
    [self.calendarView selectDate:today];
    NSLog(@"go back to today");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma  mark -
#pragma mark JBUintViewDelegate

- (CGFloat)heightOfUnitTileViewsInUnitView:(JBUnitView *)unitView
{
    return ((self.view.bounds.size.height-30-44)/6);
}

- (CGFloat)widthOfUnitTileViewsInUnitView:(JBUnitView *)unitView
{
    return (self.view.bounds.size.width/7);
}

- (UIView *)weekdaysBarViewInUnitView:(JBUnitView *)unitView
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"weekdaysBarView"]];
    imageView.frame=CGRectMake(0, 0, 320, 30);
    return imageView;
}

- (JBUnitTileView *)unitTileViewInUnitView:(JBUnitView *)unitView
{
    return [[BCUnitTitleView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width/7, (self.view.bounds.size.height-44-30)/6)];
}

- (void)unitView:(JBUnitView *)unitView NumberOfEventsInCalendarDate:(JBCalendarDate *)calendarDate WithCompletedBlock:(void (^)(NSInteger eventCount))completedBlock
{   //查询数据库，并且返回事件数目
    NSDate* date=[[calendarDate nsDate] followingDay];
    NSInteger numberOfMemos=[[BCDateBase defaultBase] theNumberOfNotesOnDay:date];
    completedBlock(numberOfMemos);
}

-(void)tappedOnUnitTitleView:(JBUnitTileView*)uintTitleView InUintView:(JBUnitView *)unitView
{
    //推出当日备忘录页面
    BCNoteDisplayViewController* noteVC=[[BCNoteDisplayViewController alloc]init];
    NSDate* date=[[uintTitleView.date nsDate] followingDay];
        self.displayDate=date;
        [noteVC setDate:date];
        [self.navigationController pushViewController:noteVC animated:YES];
}

-(void)unitView:(JBUnitView *)unitView SelectedDate:(NSDate *)date
{
    self.displayDate=date;
}

#pragma mark -
#pragma mark DatePickerControllerDelegate Method
-(void)datePickerController:(BCDatePickerController*) datePickerController   FinishedWithSelectedDate:(NSDate*) selectedDate
{
    [self.calendarView selectDate:selectedDate];
        self.displayDate=selectedDate;
        [self.calendarView selectDate:selectedDate];
}
@end
